package com.test.demo.controller;

import com.google.gson.Gson;
import com.test.demo.po.Strategy;
import com.test.demo.service.StrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Cenoo on 2017/7/3.
 */
@Controller
@RequestMapping(value = "/Strategy")
public class StrategyController {

    @Autowired
    private StrategyService strategyService;

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public Object search(HttpServletRequest request) {
        String location = request.getParameter("location");
        try {
            List<Strategy> strategies = strategyService.selectByLocation(location);
            Gson gson = new Gson();
            if (strategies != null) {
                String json = gson.toJson(strategies);
                return json;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }
}
